"""
@author: z
@file: shopping.py.py
@time: 2022/11/9 21:12
"""

user_dic = {}
shop_dic = {"显卡": 14999, "cpu": 6666}

# 欢迎语构图
def welcome():
    print("|" + "-" * 63 + "|")
    print("|" + "*" * 24 + "欢迎光临，三创购物" + "*" * 24 + "|")
    print("|" + "-" * 63 + "|")

# 读文件，将文件中的用户信息读出来加载到内存中
def read_file():
    with open("shopping_user", "r+") as f:
        for line in f.readlines():
            line = line.replace("\n", "")
            user_list = line.split()
            # dic[key] = value
            user_dic[user_list[0]] = {"pass": user_list[1], "money": user_list[2]}
    return user_dic
# print(user_dic)

# 注册
def register():
    check = input("是否需要注册账号 (y)：")
    if check == "y":
        zhuce_user = input("请输入注册的用户名：")
        zhuce_pass = input("请输入注册的密码：")
        print("\n")
        if zhuce_user in user_dic:
            print("用户已存在！")
        else:
            user_dic[zhuce_user] = {"pass": zhuce_pass, "money": "0"}

def func():
    user = input("请输入用户名：")
    # 如果user在字典里存在，进行下一步
    if user in user_dic:
        pwd = input("请输入密码：")
        print("\n")
        # 怎么让变量key去找到他自己的变量value
        if pwd == user_dic[user]["pass"]:
            for i in range(3):
                option = input("请选择下列选项：\n    <1> 显示所有商品和价格\n    <2> 显示当前用户余额\n    <3> 退出 \n")
                if option == "1":
                    print(shop_dic)
                elif option == "2":
                    print("当前余额为：" + user_dic[user]["money"])
                elif option == "3":
                    return -1
                else:
                    print("请正确输入选项！")
        else:
            print("您的密码有误，请重新输入！")
    # 如果不存在，退出
    else:
        print("系统没有匹配到对应的账号")

welcome()
read_file()
register()
func()